﻿package com.ufenlei.play {
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	import com.ufenlei.tool.RoundRectangle;

	/**
	 * @author bbn
	 */
	public class Match extends Sprite {
		private var timer:Timer;
		private var _width:uint;
		private var _height:uint;
		private var _cp:P = new P(0,0);
		private var _fa:Number;
		private var _fc:uint;
		private var _cr:uint;
		private var _lt:uint;
		private var _lc:uint;
		private var _la:uint;
		private var t:int;
		private var oh:Array = new Array();
		private var c:Sprite;
		public function Match(w:uint,h:uint,speed:Number,rs:uint,
		                      lt:uint=1,lc:uint = 0x000000, la:Number = 1.0,
		                      fc:uint = 0x000000, fa:Number = 1.0) {
			
			_width = w;
			_height = h;
			_cr = rs;
			_fa = fa;
			_fc = fc;
			_la =la;
			_lt = lt;
			_lc = lc;
			var rr:RoundRectangle = new RoundRectangle(w,h,1.0,0x333333);
			this.addChild(rr);
			this.c = new Sprite();
			c.y = h/2;
			addChild(c);
			this.graphics.lineStyle(_lt,_lc,_la);
			this.drawCircle(_cp);
			timer = new Timer(speed,100);
			timer.addEventListener(TimerEvent.TIMER, onTimer);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
			timer.start();
		}
		private function onTimerComplete(evt:TimerEvent):void{
			trace("onTimerComplete",evt);
			timer.reset();
			this.c.graphics.clear();
			timer.removeEventListener(TimerEvent.TIMER, onTimer);
			timer.addEventListener(TimerEvent.TIMER, onHistory);
			timer.start();
		}
		private function onHistory(evt:TimerEvent):void{
			_cp = oh.shift();
			draw();
		}
		private function onTimer(evt:TimerEvent):void{
			//trace(this.mouseX,this.mouseY - c.y);
			//trace(evt);
			//var p:int = 5;
			var mx:int = this.mouseX;
			var my:int  = this.mouseY - c.y;
//			var dx:int = mx - _cp.x;
//			var dy:int = my - _cp.y;
//			var pk:Number = Math.max(Math.sqrt((dx*dx + dy*dy))* 0.1, 6);; 
//			var a:Number;
//			if( dx == 0 ){
//				if(dy>0){
//					a = Math.PI/2;
//				}else{
//					a = -Math.PI/2;
//				}
//			}else{
//				a = Math.atan(dy/dx);
//				if(dx<0) a = a + Math.PI;
//			}
//			_cp.x += pk*Math.cos(a);
//			_cp.y += pk*Math.sin(a);
            _cp.x = mx;
            _cp.y = my;
            oh.push( _cp.clone() );
			draw();
		}
		private function drawCircle(p:P):void{
			if(_fa > 0 && _cr > 1 ){
				this.c.graphics.lineStyle(0);
				this.c.graphics.beginFill(_fc,_fa);
				this.c.graphics.drawCircle(p.x, p.y, _cr);
				this.c.graphics.endFill();
				this.c.graphics.lineStyle(_lt,_lc,_la);
			}
		}
		private function draw():void{
			//_cp = new P(t*5, Math.sin(t/10)*300);
			//t++;
			//this.graphics.lineTo(_cp.x, _cp.y);			
			//var p:P = randomP;
			if(_lt>=1 && _la>0){
				this.c.graphics.lineTo(_cp.x,_cp.y);
				//this.c.graphics.curveTo(p.x, p.y, _cp.x, _cp.y);
			}
			this.drawCircle(_cp);
		}
		public function get randomP():P{
			return new P(Math.floor( Math.random()*_width), Math.floor( Math.random()*_height));
		}
		public function get randomColor():uint{
			return uint(Math.floor(0xffffff * Math.random()));
		}
	}
}
class P {
	public var x:int;
	public var y:int;
	public function P(px:int = 0, py:int = 0){
		this.x = px;
		this.y = py;	
	}
	public function clone():P{
		return new P(x,y);
	}
}

